<template>
	<!-- 修改密码 -->

	<view style="background-color: white; height: 100vh;">

		<u-navbar title-color="#333" back-icon-color="#333" :border-bottom="false" :is-fixed="true"
			:background="{background:'transparent'}" :is-back="true">

		</u-navbar>

		<cxView padding-top="30" marginLeft="50" marginRight="50">
			<cxView size="36" font-weight="700">修改密码</cxView>


			<cxFormInput placeholder="请输入原密码" type="text" :background="$cx.color.cxBgColor" padding="10 20"
				margin="50 0 0 0" borderRadius="20" v-model="model.oldPwd"></cxFormInput>



			<cxFormInput placeholder="请输入新密码" type="password" :background="$cx.color.cxBgColor" padding="10 20"
				margin="30 0 0 0" borderRadius="20" v-model="model.newPwd"></cxFormInput>

			<cxFormInput placeholder="请输入确认密码" type="password" :background="$cx.color.cxBgColor" padding="10 20"
				margin="30 0 0 0" borderRadius="20" v-model="model.confirmPwd"></cxFormInput>



			<cxButton @confirm="update" height="80" marginTop="30" margin="20rxp" :color="$cx.color.cxMainColor"
				:plain="true">确定重置</cxButton>


		</cxView>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				model: {
					oldPwd: '',
					newPwd: '',
					confirmPwd: '',

				},
				show: false,
				code: '',
				isPwdLogin: true,
				codeTips: '',


			}
		},
		onReady() {

		},
		methods: {

			codeChange(text) {
				this.codeTips = text;
			},
			// 获取验证码
			async getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					let status = await this.$sendMsg(this.model.userTel);
					if (status) {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}
					// setTimeout(() => {
					// 	uni.hideLoading();
					// 	// 这里此提示会被this.start()方法中的提示覆盖
					// 	this.$u.toast('验证码已发送');
					// 	// 通知验证码组件内部开始倒计时
					// 	this.$refs.uCode.start();
					// }, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			// 修改
			update() {
				let status = this.validate();
				if (!status) {
					return
				}

				this.model.userId = this.$userInfo().userId;

				this.$u.api.updatePwd(this.model).then(res => {
					if (res.code == 200) {

						uni.showToast({
							icon: 'none',
							title: '修改成功',
							duration: 2000,
						})

						setTimeout(res => {
							uni.navigateBack({
								delta: 100
							})
						}, 1000)

					}
				})



			},
			// 返回登录
			login() {
				this.show = false;
				uni.navigateBack();
			},
			validate() {
				let loginRules = [{
						name: 'oldPwd',
						type: 'required',
						errmsg: '请输入原密码'
					},
					{
						name: 'newPwd',
						type: 'required',
						errmsg: '请输入密码'
					},
					{
						name: 'confirmPwd',
						type: 'eq',
						eqName: 'newPwd',
						required: true,
						errmsg: '两次输入密码不一致'
					},


				]
				let result = uni.$cx.validate(this.model, loginRules);

				if (!result.isOk) {
					uni.showToast({
						icon: 'none',
						title: result.errmsg
					})
					return false
				} else {
					return true;
				}

			}

		},

	}
</script>

<style>

</style>